<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>
        项目名称-添加
    </title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="../../css/main.css" media="all">
    <script type="text/javascript" src="../../lib/loading/okLoading.js"></script>
    <link rel="stylesheet" href="../../css/bootstrap.css">
    <link rel="stylesheet" href="../../lib/layui/css/layui_2.5.7.css">
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">

    <style>
        .layui-table-page select {
            height: 22px;
        }
        #container {
            height: 85%;
            width: 100%;
        }

        .amap-icon img {
            width: 25px;
            height: 34px;
        }

        .amap-marker-label{
            border: 0;
            background-color: transparent;
        }

        .info{
            position: relative;
            top: 0;
            right: 0;
            min-width: 0;
        }
        .amap-marker-label{background-color: #fff; padding: 10px; color: red;}
    </style>
</head>

<body>
<div class="layui-tab-content">
    <div class="modal-body">
        <div class="row">
            <form action="" class="layui-form">
                <div class="nav-tabs-custom layui-form-pane">
                    <ul class="nav nav-tabs">
                        <li class="active"><a href="#tab_1" data-toggle="tab" id="mapInfo">基本信息</a></li>
                        <li><a href="#tab_4" data-toggle="tab" onclick="aa()">项目位置 </a></li>

                    </ul>
                    <div class="tab-content" style="padding: 0;">
                        <div class="tab-pane active" id="tab_1">
                            <h3 style="text-align: left;">基本信息:  </h3>
                        <div class="layui-form-item" id="area-picker">
                            <div class="layui-form-label">省市地区</div>
                            <div class="layui-input-inline" style="width: 200px;">
                                <select name="province" class="province-selector" lay-filter="province-1">
                                    <option value="">请选择省</option>
                                </select>
                            </div>
                            <div class="layui-input-inline" style="width: 200px;">
                                <select name="city" class="city-selector" lay-filter="city-1">
                                    <option value="">请选择市</option>
                                </select>
                            </div>
                            <div class="layui-input-inline" style="width: 200px;">
                                <select name="county" class="county-selector" lay-filter="county-1">
                                    <option value="">请选择区</option>
                                </select>
                            </div>
                        </div>
                        <input  autocomplete="off"  type="hidden" name="areaString" id="areaString" >


                        <div class="layui-form-item">
                            <label class="layui-form-label">项目名称</label>
                            <div class="layui-input-block">
                                <input  autocomplete="off"  type="text" class="layui-input" id="name" name="name" placeholder="项目名称">
                            </div>
                        </div>
                       <!-- <div class="layui-form-item">
                            <label class="layui-form-label">街道地址</label>
                            <div class="layui-input-block">
                                <input  autocomplete="off"  type="text" class="layui-input" id="address" name="address" placeholder="街道地址">
                            </div>
                        </div>-->
                        <div class="layui-form-item">
                            <label class="layui-form-label">值班电话</label>
                            <div class="layui-input-block">
                                <input  autocomplete="off"  type="text" class="layui-input" name="tel" placeholder="值班电话">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">使用单位</label>
                            <div class="layui-input-block">
                                <select class="form-control" name="companyId">
                                    <option value="">---</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">维保负责人</label>
                            <div class="layui-input-block">
                                <select class="form-control" name="personId">
                                    <option value="">---</option>
                                </select>

                            </div>
                        </div>



                    </div>
                        <div class="tab-pane" id="tab_4" style="height: 460px;overflow: hidden;">
                            <div id="mapBox" style=" position: relative;">

                                <div class="layui-form-item">
                                    <label class="layui-form-label">详细地址</label>
                                    <div class="layui-input-block">

                                        <input  autocomplete="off"  type="text" id="detailaddress" readonly  class="layui-input"  name="detailaddress" placeholder="详细地址">
                                        <input  autocomplete="off"  type="hidden" id="addressLongitude" name="addressLongitude">
                                        <input  autocomplete="off"  type="hidden" id="addressLatitude" name="addressLatitude">
                                    </div>
                                </div>

                            </div>
                            <div id="container">

                            </div>
                            <div id="tip">
                                <input  autocomplete="off"  type="text" id="suggestId" name="suggestId" placeholder="搜索关键字"/>
                                <div id="searchResultPanel" style="border:1px solid #C0C0C0;width:150px;height:auto; display:none;"></div>
                            </div>

                        </div>
                </div>

            </div>
            <div style="text-align: center" class="layui-form-item">
                <button class="layui-btn"
                        style="border: 0; width: 120px; border-radius: 10px;" type="button" lay-filter="sub" lay-submit="sub">
                    保存
                </button>
            </div>
            </form>
</div></div></div>
</body>

<script type="text/javascript" src="../../js/jquery.min.js"></script>
<script type="text/javascript" src="../../js/myAjax.js"></script>
<script type="text/javascript" src="../../js/pub.js"></script>
<script type="text/javascript" src="../../lib/layui/layui2.5.7.js"></script>
<script type="text/javascript" src="../../js/vue.min.js"></script>
<!-- <script src="lib/layui/layui.js" charset="utf-8"></script> -->
<script src="../../js/x-layui.js" charset="utf-8"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.3.5/js/bootstrap.min.js"></script>


<script>
    //配置插件目录
    layui.config({
        base: '../../js/'
        , version: '1.0'
    });
</script>
<script>
    layui.use(['laydate', 'element', 'laypage', 'layer', 'form'], function () {
        $ = layui.jquery;//jquery
        laydate = layui.laydate;//日期插件
        lement = layui.element;//面包导航
        laypage = layui.laypage;//分页
        layer = layui.layer;//弹出层
        okLoading.close($);
    });
</script>

<script>
    layui.use(['layer', 'form','layarea'],
        function () {
            var form = layui.form;

            var layarea = layui.layarea;

            layarea.render({
                elem: '#area-picker',

                change: function (res) {
                    //选择结果
                    console.log(res);
                    if (res.province != "") {
                        $("#areaString").val(res.province + "," + res.city + "," + res.county);
                    } else {
                        $("#areaString").val("");
                    }

                }
            });


            let request = {
                url: 'user/getMaintenanceUser',
                data: {
                    userId: $userId,
                },
                method: "get",
            };
            sendRequest(request, (res) => {

                let x = res;

                $.each(x, function (index, item) {
                    let opt = $("<option value=" + item.id + ">" + item.nickname + "</option>");
                    $("select[name='personId']").append(opt);
                });
                form.render('select');

            });
            let request2 = {
                url: '/maintenance/company/findAll',
                data: {
                    userId: $userId,
                },
                method: "get",
            };
            sendRequest(request2, (res) => {
                console.log(res);
                let d = res.data;
                $.each(d, function (index, item) {
                    let opt = $("<option value=" + item.id + ">" + item.name + "</option>");
                    $("select[name='companyId']").append(opt);
                });
                form.render('select');
            });

            form.on('submit(sub)',
                function (data) {
                    var layer = layui.layer;
                    var areaString = $("#areaString").val();
                    if (areaString == '') {
                        layer.alert("省市区不能为空", {
                            icon: 5
                        })
                        return false
                    }

                    var name = $("#name").val();
                    var address = $("#address").val();
                    var detailaddress = $("#detailaddress").val();

                    if (name == '') {
                        layer.alert("项目名称不能为空", {
                            icon: 5
                        })
                        return false
                    }

                    var companyId = $("select[name=companyId]").val();
                    if (companyId == '') {
                        layer.alert("使用单位不能为空", {
                            icon: 5
                        })
                        return false
                    }
                    var personId = $("select[name=personId]").val();
                    if (personId == '') {
                        layer.alert("维保负责人不能为空", {
                            icon: 5
                        })
                        return false
                    }
                    if (detailaddress == '') {
                        layer.alert("详细地址不能为空", {
                            icon: 5
                        })
                        return false
                    }

                    let form = data.field;

                    form.maintenancepersonIdstring =","+$("select[name=personId]").val()+",";
                    form.userId=$userId;
                    form.landmark=$("#addressLongitude").val()+","+$("#addressLatitude").val();

                    let request = {
                        url: '/maintenance/project/add',
                        data: form,
                        method: "post",
                    };
                    sendRequest(request, (data) => {


                            if (data.code === 0) {
                                layer.alert("操作成功", {
                                        icon: 6
                                    },
                                    function () {
                                        var index = parent.layer.getFrameIndex(window.name);
                                        parent.layer.close(index);
                                        parent.reload();


                                    });
                            } else {
                                layer.alert(data.msg, {
                                    icon: 5
                                })
                            }


                    });
                });


        });

</script>

<script type="text/javascript" src="//api.map.baidu.com/api?v=2.0&ak=jA0VzOF5uYehineGKLNc1FBIPGDSyO7R"></script>
<script>

    function G(id) {
        return document.getElementById(id);
    }

    function aa(){


        var map = new BMap.Map("container");
        map.centerAndZoom("淮北",12);
        map.enableScrollWheelZoom(true);

        var ac = new BMap.Autocomplete(    //建立一个自动完成的对象
            {
                "input" : "suggestId",
                "location" : map
            });

        ac.addEventListener("onhighlight", function(e) {  //鼠标放在下拉列表上的事件
            var str = "";
            var _value = e.fromitem.value;
            var value = "";
            if (e.fromitem.index > -1) {
                value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
            }
            str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;

            value = "";
            if (e.toitem.index > -1) {
                _value = e.toitem.value;
                value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
            }
            str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
            G("searchResultPanel").innerHTML = str;
        });

        var myValue;
        ac.addEventListener("onconfirm", function(e) {    //鼠标点击下拉列表后的事件
            var _value = e.item.value;
            myValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
            G("searchResultPanel").innerHTML ="onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;

            setPlace();
        });
        var geoc = new BMap.Geocoder();
        map.addEventListener('click', function (e) {

            map.clearOverlays();
            $("#addressLongitude").val(e.point.lng);
            $("#addressLatitude").val(e.point.lat);


            var marker1 = new BMap.Marker(new BMap.Point(e.point.lng, e.point.lat));
            map.addOverlay(marker1);

            var pt =e.point;
            geoc.getLocation(pt, function(rs){

                if(rs.surroundingPois.length==0){
                    $('#detailaddress').val(rs.address);
                }else{
                    $('#detailaddress').val(rs.address+rs.surroundingPois[0].title);
                }

            })
        });

        function setPlace(){
            map.clearOverlays();    //清除地图上所有覆盖物
            function myFun(){
                var pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
                map.centerAndZoom(pp, 20);
                //添加标注
            }
            var local = new BMap.LocalSearch(map, { //智能搜索
                onSearchComplete: myFun
            });
            local.search(myValue);
        }
    }




</script>

<style>
    #container{position: relative;}

    #tip {
        background-color: #ddf;
        color: #333;
        border: 1px solid silver;
        box-shadow: 3px 4px 3px 0px silver;
        position: absolute;
        top: 120px;
        right: 10px;
        border-radius: 5px;
        overflow: hidden;
        line-height: 20px;
        z-index: 99999;
    }
    #tip input[type="text"] {
        height: 25px;
        border: 0;
        padding-left: 5px;
        width: 280px;
        border-radius: 3px;
        outline: none;
    }
</style>

</html>
